# gen_retention_plots makefile

TASK_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))

include ../../config.mk

# Scripts
BUILD_RETENTION_PANEL := $(TASK_DIR)/src/01_build_retention_panel.R

CALCULATE_RETENTION := $(TASK_DIR)/src/02_calculate_retention.R

PLOT_RETENTION := $(TASK_DIR)/src/03_plot_retention.R

# Inputs (that have not already been specified in config.mk)
CONFIG_FILE := $(TASK_DIR)/hand/config.yaml

# Outputs (intermediary files and final figures)
OUT_RETENTION_PANEL := $(OUT_DIR)/payroll_rentention_panel.csv

OUT_WEEKLY_RETENTION := $(OUT_DIR)/retention_by_week.csv

OUT_PRECOVID_PLOT := $(OUT_DIR)/retention_precovid_combined_up_to_date.jpg

OUT_FULLPERIOD_PLOT := $(OUT_DIR)/retention_fullperiod_combined_up_to_date.jpg

# Recipes

$(OUT_RETENTION_PANEL): $(BUILD_RETENTION_PANEL) $(CONFIG_FILE) $(XSECTION_ANALYSIS_FILE) $(PAYROLL_FILE)
	@mkdir -p $(OUT_DIR)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--PAYROLL_FILE=$(PAYROLL_FILE) \
		--OUT_RETENTION_PANEL=$(OUT_RETENTION_PANEL)

$(OUT_WEEKLY_RETENTION): $(CALCULATE_RETENTION) $(TASK_FNS) $(CONFIG_FILE) $(PROJECT_CONFIG) $(XSECTION_ANALYSIS_FILE) $(OUT_RETENTION_PANEL)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--OUT_RETENTION_PANEL=$(OUT_RETENTION_PANEL) \
		--OUT_WEEKLY_RETENTION=$(OUT_WEEKLY_RETENTION)


$(OUT_FULLPERIOD_PLOT): $(PLOT_RETENTION) $(TASK_FNS) $(CONFIG_FILE) $(OUT_WEEKLY_RETENTION)
	@mkdir -p $(PAPER_OUT_DIR)
	@$(R_script) $< \
		--CONFIG_FILE=$(CONFIG_FILE) \
		--OUT_WEEKLY_RETENTION=$(OUT_WEEKLY_RETENTION) \
		--OUT_FULLPERIOD_PLOT=$(OUT_FULLPERIOD_PLOT)	\
		--OUT_PRECOVID_PLOT=$(OUT_PRECOVID_PLOT)
	@cp $(OUT_DIR)/*up_to_date.jpg $(PAPER_OUT_DIR)


.PHONY: all clean
.DEFAULT_GOAL=all

all: $(OUT_RETENTION_PANEL) $(OUT_WEEKLY_RETENTION) $(OUT_FULLPERIOD_PLOT)

clean:
	rm -rf $(OUT_DIR)/*

